package edu.hawaii.ics414.blackjack.player;

import edu.hawaii.ics414.blackjack.card.Card;
import edu.hawaii.ics414.blackjack.card.GameDeck;

/**
 * The dealer at the table.
 * 
 * @author Branden Ogata
 *
 */

public class Dealer extends NovicePlayer
{
  /**
   * Indicates whether to show all cards or just the hole card.
   * 
   */
  private boolean showCards;
  
  /**
   * Creates a new Dealer.
   * 
   * @param money The int indicating the amount of money that the Dealer should start with.
   * 
   */
  
  public Dealer(int money)
  {
    super(money);
    this.setBet(0, 0);
    this.lastBet[0] = 0;
  }

  /**
   * Returns the move of this Dealer.
   * 
   * @param deck The GameDeck containing the cards to use.
   * @param hand The int indicating the hand to play on.
   * @param hole The Card that the dealer has showing.
   * 
   * @return action The Action that this Dealer intends to play.
   * 
   */
  
  @Override
  public Action getMove(GameDeck deck, int hand, Card hole)
  {
    // Calls the superclass method, which in most cases will be the proper move for the dealer
    Action action = super.getMove(deck, hand, hole);
    
    // If NovicePlayer would split or double down, then alter behavior
    if ((action.equals(Action.DOUBLE_DOWN)) || (action.equals(Action.SPLIT)))
    {
      action = (this.sum(hand) <= 17) ? (Action.HIT) : (Action.STAND);
    }
    
    return action;
  }  
  
  
  /**
   * Returns a String representation of the cards for this Dealer.
   * This should show one card face up, with all the rest face down.
   * 
   * @return desc A String representing the cards that this Dealer has.
   * 
   */
  
  @Override
  public String getCards()
  {
    String desc = "";
    
    Card[] hand = this.hands[0];
    desc += String.format("(Value: %d", ((this.showCards) ? 
                                                       (this.sum(0)) : 
                                                       (this.getHoleCard().getValue()))); 
    
    if (this.sum(0) > 21) 
    {
      desc += "BUSTED";
    }
    else if ((this.sum(0) == 21) && (this.countNumberOfCards(0) == 2) && (this.showCards))
    {
      desc += "BLACKJACK";
    }
    
    desc += ")  -  ";
    
    // Add each card to the String
    for (int j = 0; j < hand.length; j++)
    {
      Card card = hand[j];
      
      if (card != null)
      {
        if ((j == 0) || (this.showCards))
        {
          desc += card.toString() + " ";
        }
        else
        {
          desc += "XX ";
        }
      }
    }
      
    if (hand[0] != null)
    {
      desc += "\n";
    }
        
    return desc;
  }

  /**
   * Clears all hands for this Dealer.
   * Used at the end of a round; also resets the behavior for showing cards.
   * 
   */
  
  @Override
  public void clear()
  {
    super.clear();
    this.showCards = false;
  }
  
  /**
   * Returns the first card of this Dealer.
   * Necessary to show the hole card to other players.
   * 
   * @return The Card in the first slot of the first hand.
   * 
   */
  
  public Card getHoleCard()
  {
    return this.hands[0][0];
  }
  
  /**
   * Sets whether to show all cards in the hand of this Dealer.
   * 
   * @param showCards The boolean indicating whether to show all of the cards that this Dealer has.
   * 
   */
  
  public void setShowCards(boolean showCards)
  {
    this.showCards = showCards;
  }
}
